package com.rain.search.controller;

import com.rain.search.pojo.Article;
import com.rain.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @auther 德哲
 * @date 2019/4/28 21:19.
 */
@RestController
@RequestMapping("/article")
public class ArticleSearchController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article){
        articleService.save(article);
        return new Result(true, StatusCode.OK,"执行成功");
    }

    @RequestMapping(value = "/search/{keywords}/{page}/{size}",method = RequestMethod.GET)
    public Result findByKeywords(@PathVariable("keywords") String keywords,@PathVariable("page") int page,@PathVariable("size") int size){
        Page<Article> articlePage = articleService.findByKeywords(keywords, page, size);
        PageResult<Article> articlePageResult = new PageResult<>(articlePage.getTotalElements(), articlePage.getContent());
        return new Result(true, StatusCode.OK,"执行成功",articlePageResult);
    }

    /*public static void main(String[] args) throws IOException {
        //执行本地cmd命令
        Runtime.getRuntime().exec("notepad");
    }*/
}
